<template>
  <div>

    <div class="layout-content">
      <el-form ref="form" :inline="true" label-position="top" size="mini" label-width="100px">
        <el-form-item label="团购标题" prop="title">
          <el-input style="width:120px;" placeholder="请输入团购标题" />
        </el-form-item>
        <el-form-item label="描述" prop="title">
          <el-input style="width:120px;" placeholder="请输入团购描述" />
        </el-form-item>
        <el-form-item label="封面1" prop="thumb">
        <!-- <el-upload
          class="avatar-uploader"
          list-type="picture-card"
          :action="upload_url+'?id='+index"
          :show-file-list="false"
          :on-success="handleAvatarSuccess_thumb"
        >
          <img v-if="item.thumb" :src="item.thumb" >
          <i v-else class="el-icon-plus" />
        </el-upload> -->
        </el-form-item>
        <el-form-item label="封面2" prop="thumb">
        <!-- <el-upload
          class="avatar-uploader"
          list-type="picture-card"
          :action="upload_url+'?id='+index"
          :show-file-list="false"
          :on-success="handleAvatarSuccess_thumb"
        >
          <img v-if="item.thumb" :src="item.thumb" >
          <i v-else class="el-icon-plus" />
        </el-upload> -->
        </el-form-item>
        <el-form-item label="操作">
          <el-tooltip class="item" effect="dark" content="保存修改 " placement="right">
            <el-button type="success" icon="el-icon-check" circle @click="onSubmit('form')" />
          </el-tooltip>
        </el-form-item>
      </el-form>
    </div>
    <control :config="config" />
    <div class="layout-header">
      <el-button
        type="primary"
        icon="el-icon-plus"
        style="margin-left:0px"
        @click="changeEdit(0)"
      >添加</el-button>
    </div>

    <div class="layout-content">
      <el-table
        :data="tableData"
        stripe
        align="center"
        highlight-current-row
        style="width: 100%;"
      >
      <el-table-column align="center" prop="type" label="类型" width="100">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.type %2==0 ? 'primary' : 'success'"
              size="mini"
            >{{ type_arr[scope.row.type] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="title" label="团购名称" />
        <el-table-column align="center" prop="thumb" label="封面" width="80">
          <template slot-scope="scope">
            <img :src="scope.row.thumb" width="100%">
          </template>
        </el-table-column>
        <el-table-column align="center" prop="start_time" label="开始时间" width="90" />
        <el-table-column align="center" prop="end_time" label="结束时间" width="90" />
        <el-table-column align="center" prop="group_time" label="拼团时间(小时)" width="80" />
        <el-table-column align="center" prop="real_price" label="价格"  />
        <el-table-column align="center" prop="people_count" label="团购人数"  />
        <el-table-column align="center" prop="group_count" label="成团数"  />
        <el-table-column align="center" prop="max_group_count" label="最大成团数" />
        <el-table-column align="center" prop="status" label="是否启用" >
          <template slot-scope="scope">
            <el-button size="mini" :type="scope.row.status === 1 ? 'primary' : 'success'" @click="change_status(scope.row.id,'status' ,scope.row.status)">
              {{ scope.row.status === 1 ? "是" : "否" }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="telephone" fixed="right" width="200" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="success" icon="el-icon-s-tools" @click="stock_click(scope.row)" />
            <el-button style="margin-left:0px;" type="primary" size="mini" icon="el-icon-edit" @click="changeEdit(scope.row.id)" />
            <el-button style="margin-left:0px;" size="mini" type="danger" icon="el-icon-delete" @click="onDel(scope.row.id)" />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      :current-page="current_page"
      :page-size="per_page"
     layout="total, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 规格 -->
    <el-dialog title="操作规格" :visible.sync="spec_dialog" height="50%" width="50%">
      <el-table
        :data="spec_tableData"
        stripe
        :loading="spec_loading"
        align="center"
        max-height="340px"
        highlight-current-row
        style="width: 100%;margin-top:-10px"
      >
        <el-table-column prop="key_name" label="规格" />
        <el-table-column prop="group_real_price" label="销售价格" width="150">
          <template slot-scope="scope">
            <el-input v-model="scope.row.group_real_price" />
          </template>
        </el-table-column>
        <el-table-column prop="group_status" label="状态" width="80">
          <template slot-scope="scope">
            <el-button
              :type="scope.row.group_status === 1 ? 'primary' : 'success'"
              size="mini"
              @click="change_status2(scope.row.id, 'group_status', scope.row.group_status)"
            >{{ scope.row.group_status === 1 ? "是" : "否" }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="spec_dialog = false">取 消</el-button>
        <el-button
          type="primary"
          @click="modify_spec_stock()"
        >保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import control from '../../../components/control/control'
export default {
  name: 'GroupBuyEdit',
  components: { control },
  data() {
    return {
      goods_id: 0,
      config: [
        { type: 'text', placeholder: '请输入团购名称', field: 'title', val: '' }
      ],
      value: '',
      searchWhere: {
        title: ''
      },
      tableData: [],
      type_arr: [],
      total: 0,
      current_page: 1,
      per_page: 14,
      spec_dialog: false,
      spec_loading: false,
      spec_tableData: []
    }
  },
  mounted() {
    this.onSearch()
  },
  methods: {
    // 修改edit值 显示编辑组件
    changeEdit(id) {
this.$store.commit('user/SET_EDITID',id)
      this.$store.commit('user/SET_EDIT',7)
    },
    // 查询
    handleSizeChange(val) {
      this.onSearch()
    },
    handleCurrentChange(val) {
      this.current_page = val
      this.onSearch()
    },
    onSearch() {
      const form = {}
      form.page = this.current_page
      form.map = this.searchWhere
      this.get('/shop/GoodsGroup/index', form, 'POST').then(e => {
        this.tableData = e.list.data
        this.type_arr = e.type_arr
        this.per_page = e.list.per_page
        this.total = e.list.total
        this.current_page = e.list.current_page
      })
    },
    // 添加
    onAdd() {},
    // 编辑
    onEdit(id) {
      console.log(id)
    },
    // 修改状态
    change_status(id, field, val) {
      this.update(id, val, field, 'GoodsGroup', 'shop').then(e => {
        e ? this.onSearch() : ''

      })
    },
    // 删除
    onDel(id) {
      this.$msg_confirm().then(e => {
        if (e) {
          this.get('/shop/GoodsGroup/del?id=' + id, this.form, 'POST').then(e => {
            this.onSearch()
          })
        }
      })
    },
    stock_click(row) {
      this.goods_id = row.id
      this.spec_info(row.id)
      this.spec_dialog = true
    },
    spec_info(id) {
      this.spec_loading = true
      const form = {
        goods_group_id: id
      }
      this.get('/shop/GoodsGroup/spec_list', form, 'POST').then(e => {
        this.spec_tableData = e.list
        this.spec_loading = false
      })
    },
    // 操作规格库存
    modify_spec_stock() {
      const form = {
        goods_group_id: this.goods_id,
        list: this.spec_tableData
      }
      this.get('/shop/GoodsGroup/spec_save', form, 'post').then(
        e => {
          this.$msg_ok('success', '操作成功')
          this.spec_dialog = false
          this.onSearch()
        }
      )
    },
    change_status2(id, field, val) {
      const form = {}
      form.id = id
      form.field = field
      val === 1 ? form.val = 0 : form.val = 1
      return this.get('/shop/GoodsGroup/spec_status', form, 'post').then(e => {
        this.$msg_ok('success', '修改成功')
        this.spec_info(this.goods_id)
      })
    }
  }
}
</script>

<style>
.layout-all {
  margin-top: 20px;
  margin-left: 10px;
}
.layout-content {
  margin-top: 20px;
}
</style>>
